import datetime
import hashlib
import secrets
import string

timestamp_bit = 31

host_max_bit = 5

auto_increase_bit = 16

total_bit = timestamp_bit + host_max_bit + auto_increase_bit

auto_increase = 0

last_timestamp = 0


def get_timestamp():
    """
    :return: 秒级时间戳
    """
    return int(datetime.datetime.now().timestamp())


def snowflake(host_id=1):
    """
    snowflake算法的简化实现
    ====================

    :param host_id:
    :return:
    """
    current_timestamp = get_timestamp()  # 16

    max_host_id = 2 ** host_max_bit
    if host_id >= max_host_id:
        raise Exception(f'host_id out of range, {host_id}; max: {max_host_id}')

    result = (current_timestamp << host_max_bit) + host_id

    global auto_increase, last_timestamp

    if current_timestamp == last_timestamp:
        auto_increase += 1

        if auto_increase >= 2 ** auto_increase_bit:
            raise Exception('boom, out of range')
    else:
        auto_increase = 0

    last_timestamp = current_timestamp

    result = (result << auto_increase_bit) + auto_increase

    assert result.bit_length() == total_bit

    return result


def create_token():
    val = snowflake()
    return str(hashlib.md5(str(val).encode("utf-8")).hexdigest())


def generate_random_code(length):
    """
    生产邀请码
    """
    # 生成包含大写字母和数字的字符集
    characters = string.ascii_uppercase + string.digits
    # 从字符集中随机选择指定长度的字符并拼接起来
    code = ''.join(secrets.choice(characters) for _ in range(length))
    return code


if __name__ == '__main__':
    print(generate_random_code(10))